using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using MimeCloud.AssetManagement.Business.Interface;
using MimeCloud.AssetManagement.Editor.AssetCreation.Repository;
using MimeCloud.AssetManagement.Editor;

namespace MimeCloud.AssetManagement.Editor.AssetCreation
{
    public class Video
    {
        public static IAssetContent CreateContent(string fileLocation, IAssetContentType sourceType)
        {
            var result = LocallyStoredAssetContent.Create(LocallyStoredAssetContent.LocalStoreType.FileSystem);
            (result.AssetContentRepositoryList[0] as LocalFileStoreSingleAssetRepository).URLPrefix = fileLocation;
            result.AssetPath = Path.GetFileName(fileLocation);
            PopulateContent(result, sourceType,fileLocation);

            return result;
        }

        private static void PopulateContent(IAssetContent result, IAssetContentType sourceType, string fileName)
        {
            //set content type
            IAssetContentType ct = sourceType;
            if (ct == null)
                throw new ArgumentException("Video was not of a known size");
            result.AssetContentType = ct;
            result.AssetContentTypeID = ct.ID;

            //set mime type
            result.MimeType = GetMimeTypeForVideo(fileName);
            result.MimeTypeID = result.MimeType.ID;
        }

        public static IMimeType GetMimeTypeForVideo(string fileName)
        {
            string extension = Path.GetExtension(fileName);
            List<IMimeType> imageMimeTypes =
                AppCommon.Current.AllMimeTypes.Where(
                    m => m._MimeType.ToLower().Contains(extension.Trim(new char[]{'.'}).ToLowerInvariant())).ToList();
            if (imageMimeTypes.Count == 0)
                throw new Exception("Database does not support this type");
            
            return imageMimeTypes[0];
        }
    }
}
